// vars/mavenBuild.groovy
 

def call(Map config) {
    // evaluate the body block, and collect configuration into the object
    
    APP_VERSION = "${config.IMG_VERSION}"
    stage('Package') {
        mvnHome = tool 'maven' 
        
        if(config.SNAPSHOT) {  
           echo '构建开发版本'
           APP_VERSION=APP_VERSION + '-SNAPSHOT'           
        } else{
           echo '构建发布版本'           
        }
        
        MVN_OPT = config.DEPLOY ? "install deploy" : ""
        POM_NAME = config.SNAPSHOT ? config.POM : config.RELEASE_POM
        
        
        if(config.SCM){
            checkout scm 
        }else{
            git branch: "${config.GIT_BRANCH}",depth=1, credentialsId: 'chendw', url: "${config.GIT_URL}"
        }
        if (fileExists('/usr/local/jdk8')) {
            env.JAVA_HOME = '/usr/local/jdk8'
        }
        try {
            sh  """             

                java -version
                echo '更新版本号 APP_VERSION：${APP_VERSION}'
                ${mvnHome}/bin/mvn versions:set -DnewVersion=${APP_VERSION}
         
                echo '提交版本号'
                ${mvnHome}/bin/mvn versions:commit

                
                echo '构建组件'
                //${mvnHome}/bin/mvn -f ${POM_NAME}.xml -Pdocker${config.NODE_TYPE} -U -DskipTests clean package ${MVN_OPT}
                //${mvnHome}/bin/mvn -U -DskipTests clean -f ${POM_NAME}.xml -P docker${config.NODE_TYPE} ${MVN_OPT}
                ${mvnHome}/bin/mvn -f ${POM_NAME}.xml -Pdocker${config.NODE_TYPE} -U -DskipTests clean package ${MVN_OPT}
                
            """
        }
        catch (exc) {
            echo '回滚版本号'
            sh "${mvnHome}/bin/mvn versions:revert "
            throw exc
        }
    }
    
}
